﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdSetGNodeId.h"
#include "IUiView.h"
#include "IElement.h"
#include "OwnerPtr.h"
#include "IDocument.h"
#include "ElementId.h"
#include "IModelView.h"
#include "ISelection.h"
#include "IUiDocument.h"
#include "IDirectShape.h"
#include "IGraphicsNode.h"
#include "IGenericElement.h"
#include "GcmpCommandState.h"
#include "IUserTransaction.h"
#include "IGraphicsBRepBody.h"
#include "IGraphicsBRepEdge.h"
#include "IGraphicsBRepFace.h"
#include "IElementModelShape.h"
#include "IGraphicsNodeGroup.h"
#include "IPureGraphicsElement.h"
#include "IGraphicsElementShape.h"
#include "IUiDocumentViewManager.h"

#include "GRepCommandIds.h"
#include "CommandRegister.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdSetGNodeId)

CmdSetGNodeId::CmdSetGNodeId()
    :CommandBase(ID_CMD_GREP_SET_GNODE_ID)
{
}

bool CmdSetGNodeId::IsEnabled() const
{
    if (!GcmpCommandState::IsInDocumentEnvironment())
    {
        return false; // 不在项目环境则退出
    }
    // 只有只选择了一个图元时才可以执行命令
    if (ISelection* pSelection = ISelection::Get())
    {
        std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
        if (elemMaps.size() == 0)
        {
            return false;
        }
    }
    return true;
}

bool CmdSetGNodeId::IsVisible() const
{
    return GcmpCommandState::IsInDocumentEnvironment();
}

void CmdSetGNodeId::SetGGroupId(IGraphicsGroupBase* pGNodeGroup, int& nodeId)
{
    int count = pGNodeGroup->GetChildrenCount();
    for (int i = 0; i < count; i++)
    {
        IGraphicsNode* pGNode = pGNodeGroup->GetChildFW(i);
        SetGNodeId(pGNode, nodeId);
    }
}

void CmdSetGNodeId::SetGNodeId(IGraphicsNode* pGNode, int& nodeId)
{
    pGNode->SetId(GraphicsNodeId(nodeId++));
    IGraphicsNodeGroup* pGNodeGroup = quick_cast<IGraphicsNodeGroup>(pGNode);
    if (pGNodeGroup)
    {
        SetGGroupId(pGNodeGroup, nodeId);
        return;
    }
    IGraphicsBRepBody* pGBrepBody = quick_cast<IGraphicsBRepBody>(pGNode);
    if (pGBrepBody)
    {
        int edgeCount = pGBrepBody->GetEdgeCount();
        for (int i = 0;i<edgeCount;i++)
        {
            IGraphicsBRepEdge* pGBrepEdge = pGBrepBody->GetEdgeFW(i);
            pGBrepEdge->SetId(GraphicsNodeId(nodeId++));
        }
        int faceCount = pGBrepBody->GetFaceCount();
        for (int i = 0; i < faceCount; i++)
        {
            IGraphicsBRepFace* pGBrepFace = pGBrepBody->GetFaceFW(i);
            pGBrepFace->SetId(GraphicsNodeId(nodeId++));
        }
        return;
    }
}

gcmp::OwnerPtr<gcmp::IAction> CmdSetGNodeId::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败",L"GDMP",L"2024-03-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败",L"GDMP",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败",L"GDMP",L"2024-03-30");
    IUiView* pUiView = pUiDocViewMgr->GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiView, L"pUiView为空",L"GDMP",L"2024-03-30");
    IModelView* pModelView = pUiView->GetModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMP",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, L"添加GNode");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction, L"opTransaction为空",L"GDMP",L"2024-03-30");

    ISelection* pSelection = ISelection::Get();
    std::unordered_set<ElementId, ElementIdHash> elemMaps = pSelection->GetSelectedElements();
    for (ElementId id : elemMaps)
    {
        IElement* pElem = pDoc->GetElement(id);
        // 下面只支持对IPureGraphicsElement或IDirectShape设置节点标识
        IPureGraphicsElement* pPGE = quick_cast<IPureGraphicsElement>(pElem);
        if (pPGE)
        {
            IElementModelShape* pElementModelShape = pPGE->GetElementModelShape();
            DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementModelShape, L"pElementModelShape为空",L"GDMP",L"2024-03-30");
            OwnerPtr<IGraphicsElementShape> opGRep = pElementModelShape->TransferGraphicsElementShape();
            DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGRep, L"opGRep为空",L"GDMP",L"2024-03-30");
            int nodeId = 0;
            SetGGroupId(opGRep.get(), nodeId);

            pElementModelShape->SetGraphicsElementShape(TransferOwnership(opGRep));
        }
        else
        {
            IGenericElement* pGElem = quick_cast<IGenericElement>(pElem);
            if (pGElem)
            {
                IDirectShape* pDirectShape = quick_cast<IDirectShape>(pGElem->GetExternalObject());
                if (pDirectShape)
                {
                    IElementModelShape* pElementModelShape = pGElem->GetElementModelShape();
                    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElementModelShape, L"pElementModelShape为空",L"GDMP",L"2024-03-30");
                    OwnerPtr<IGraphicsElementShape> opGRep = pElementModelShape->TransferGraphicsElementShape();
                    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opGRep, L"opGRep为空",L"GDMP",L"2024-03-30");
                    int nodeId = 0;
                    SetGGroupId(opGRep.get(), nodeId);

                    pElementModelShape->SetGraphicsElementShape(TransferOwnership(opGRep));
                }
            }
        }
    }
    opTransaction->Commit();
    return nullptr;
}

